<template>
  <div>
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="80px"
      :inline="false"
      size="samll"
    >
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="文章描述" prop="description">
        <el-input  v-model="form.description"></el-input>
      </el-form-item>
      <el-form-item label="浏览量" prop="views">
        <el-input-number :min="0" v-model="form.views"></el-input-number>
      </el-form-item>
      <el-form-item label="创建人id" prop="memberId">
        <el-input v-model="form.memberId"></el-input>
      </el-form-item>
      <el-form-item label="所属分类" prop="categoryId">
        <el-select v-model="form.categoryId" clearable filterable>
          <el-option
            label="-请选择-"
            value=""
          />
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文章内容" prop="content">
         <el-input type="textarea" :rows="3"  v-model="form.content"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number :min="0" v-model="form.sort"></el-input-number>
      </el-form-item>
      <el-form-item label="是否精品" prop="boutique">
        <el-radio v-model="form.boutique" :label="0">普通</el-radio>
        <el-radio v-model="form.boutique" :label="1">精品</el-radio>
      </el-form-item>
      <el-form-item label="是否展示" prop="status">
        <el-radio v-model="form.status" :label="0">不展示</el-radio>
        <el-radio v-model="form.status" :label="1">展示</el-radio>
      </el-form-item>
      <el-form-item style="display: flex; justify-content: flex-end">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import blogApi from "@/api/blog";
import categoryApi from '@/api/category'
export default {
  name: "blogSave",
  props: {
    blogId: {
      type: String,
      default: "",
    },
    close: {
      type: Boolean,
      defalut: false,
    },
  },
 
  data() {
    return {
      options: [],
      form: {
        id:'',
        title:'',
        description:'',
        views:0,
        memberId:'1',
        categoryId:'',
        boutique:0,
        status:1,
        sort:0
      },
      rules: {
        title: [{ required: true, message: "文章标题不能为空", trigger: "blur" }],
        description: [{ required: true, message: "文章描述不能为空", trigger: "blur" }],
        categoryId: [{ required: true, message: "所属分类不能为空", trigger: "change" }],
      },
    };
  },
   watch: {
    close(newValue, oldValue) {
      this.form = {
        id:'',
        title:'',
        description:'',
        views:0,
        memberId:'1',
        categoryId:'',
        boutique:0,
        status:1,
      };
    },
    blogId(newValue, oldValue){
      if (this.blogId) this.getInfo(this.blogId);
    }
  },
  created() {
    this.getCategory()
    if (this.blogId) this.getInfo(this.blogId);
  },
  methods: {
    // 获取分类
    async getCategory(){
      let res = await categoryApi.list({pageNum:1,pageSize:99});
      if(res.success){
        this.options = res.data.records
      }else{
        this.options = []
        return this.$message.error(res.msg)
      }
    },
    // 获取数据
    async getInfo(id) {
      let res = await blogApi.info(id);
      if (res.success) {
        this.form = res.data.data;
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 保存
    onSubmit() {
      this.$refs.form.validate((v) => {
        if (this.blogId) {
          this.handleUpdate();
        } else {
          this.handleAdd();
        }
      });
    },
    // 添加
    async handleAdd() {
      let res = await blogApi.add(this.form);
      if (res.success) {
        this.$message.success("添加成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 修改
    async handleUpdate() {
      let res = await blogApi.update(this.form);
      if (res.success) {
        this.$message.success("修改成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },

    handleClose() {
      this.$emit("done");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>